package me.zhengjie.tools.domain

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.bean.copier.CopyOptions
import com.fasterxml.jackson.annotation.JsonTypeName
import io.swagger.v3.oas.annotations.media.Schema
import me.zhengjie.common.utils.JacksonUtil
import me.zhengjie.modules.system.domain.Area
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.Id
import jakarta.persistence.Table
import jakarta.validation.constraints.NotBlank
import org.apache.commons.lang3.builder.ToStringBuilder
import org.springframework.data.redis.core.RedisHash

/**
 * 支付宝配置类
 * @author Kuki Wu
 * @date 2018-12-31
 */
@Entity
@Table(name = "tool_alipay_config")
@JsonTypeName("AlipayConfig")
@RedisHash("alipayconfig")
data class AlipayConfig (
    @Id
    @Schema(description ="id")
    @Column(name = "config_id")
    var id: Long? = null,

    // 应用ID,APPID，收款账号既是APPID对应支付宝账号
    @Schema(description ="应用ID,APPID，收款账号既是APPID对应支付宝账号")
    @Column(name = "app_id")
    var appID: @NotBlank String? = null,

    // 商户私钥，您的PKCS8格式RSA2私钥
    @Schema(description ="商户私钥，您的PKCS8格式RSA2私钥")
    @Column(name = "private_key", columnDefinition = "text")
     var privateKey: @NotBlank String? = null,

    // 支付宝公钥
    @Schema(description ="支付宝公钥")
    @Column(name = "public_key", columnDefinition = "text")
     var publicKey: @NotBlank String? = null,

    // 签名方式，固定格式
    @Schema(description ="签名方式，固定格式")
    @Column(name = "sign_type")
     var signType: String = "RSA2",

    // 支付宝开放安全地址，一般不会变
    @Schema(description ="付宝开放安全地址，一般不会变")
    @Column(name = "gateway_url")
     var gatewayUrl: String = "https://openapi.alipaydev.com/gateway.do",

    // 编码，固定格式
    @Schema(description ="编码，固定格式")
    var charset: String = "utf-8",

    // 异步通知地址
    @Schema(description ="异步通知地址")
    @Column(name = "notify_url")
     var notifyUrl: @NotBlank String? = null,

    // 订单完成后返回的页面
    @Schema(description ="订单完成后返回的页面")
    @Column(name = "return_url")
     var returnUrl: @NotBlank String? = null,

    // 类型，固定格式
    @Schema(description ="类型")
    var format: String = "JSON",

    // 商户号
    @Schema(description = "商户号")
    @Column(name = "sys_service_provider_id")
     var sysServiceProviderId: @NotBlank String? = null
){
    annotation class Update

    companion object {
        fun fromJson(json: String): AlipayConfig? {
            val obj=JacksonUtil.parseObject(json, AlipayConfig::class.java)!!
            return when(obj){
                is AlipayConfig -> obj
                else -> null
            }
        }
    }
    fun tojson():String? {
        return JacksonUtil.toJSONString(this)
    }


    fun copy(source: AlipayConfig?) {
        BeanUtil.copyProperties(source, this, CopyOptions.create().setIgnoreNullValue(true))
    }
}